home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / FIREKIT.ZIP / FLAME.C < prev    next >
C/C++ Source or Header  |  1996-05-27  |  7KB  |  313 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4. #include <malloc.h>
  5. #include <string.h>
  6.  
  7. /**********************************************************************
  8.      FIREKIT v1.0
  9.              by
  10.  
  11. John W. Ractliff
  12. 70253.3237@compuserve.com
  13. jratclif@inlink.com
  14.  
  15. 32bit DOS4GW application.
  16. Will run in Microsoft Windowss 32 bit mode.
  17. Source code in ANSI C and Turbo Assembler
  18.  
  19. The enclosed software was written by John W. Ratcliff on May 27, 1996.
  20. As of this date I am releasing this software into the public domain.
  21.  
  22. I am releasing this software into the public domain, hopefully, to make
  23. a point.  I would like to demonstrate how to write source code that is
  24. useful to other people.  I was looking at some of the various flame
  25. algorithms and source code examples and in each case they were such hard
  26. coded demos nobody could actually get them integrated into an
  27. application.
  28. ************************************************************************/
  29.  
  30. #include "flames.h"
  31.  
  32. int  CreateFlame(FLAMESPEC *f,long wid,long hit);
  33. void DestroyFlame(FLAMESPEC *f);
  34. char * LoadPicture(char *fname,char *imagepal);
  35. void FlameColorMatch(FLAMESPEC *f,unsigned char *imagepal,unsigned char *fpal);
  36.  
  37. // Different fuel feeder mask sizes.
  38. static int masktable[10] = { 0, 1, 3, 7, 15, 31, 63, 127, 255, 511 };
  39.  
  40. void main(void)
  41. {
  42.     int key,xcon=0;
  43.     FLAMESPEC f;
  44.     int fheight=80; // initial flame height.
  45.     int fwidth=320;  // initial flame width.
  46.     int fuel;
  47.     int mask=5;
  48.     char *picimage=0;
  49.     char *buildframe=0;
  50.     unsigned char imagepal[768]; // palette for the image.
  51.     unsigned char fpal[768];
  52.  
  53.     buildframe = malloc(64000); // holds scratch image buffer for background pic demo.
  54.     CreateFlame(&f,fwidth,fheight); // create flame buffers of this size.
  55.     fuel = f.fuel; // initial fuel equal to default fuel.
  56.  
  57.     FlameStart(); // turn 320x200 256 color graphics mode on DOS only!
  58.     FlamePal(fpal); // set the flame color palette, and return 8 bit rgb values in fpal
  59.  
  60.     do
  61.     {
  62.         // Change fuel amount if change is requested.
  63.         if ( f.fuel != fuel )
  64.         {
  65.             if ( f.fuel < fuel ) f.fuel++;
  66.             if ( f.fuel > fuel ) f.fuel--;
  67.         }
  68.  
  69.         FlameFrame(&f); // compute the flame frame.
  70.  
  71.         if ( picimage ) // if demonstrating merge with application screen.
  72.         {
  73.             memcpy(buildframe,picimage,64000); // copy demo app image into buildframe
  74.             FlameCopyTranslate(&f,buildframe); // copy translate flames into application memory
  75.             FlameImage(buildframe); // copy entire image DOS only routine.
  76.         }
  77.         else
  78.             FlameCopy(&f);    // Copy the flame to the video screen, DOS ONLY!
  79.  
  80.         if ( kbhit() )
  81.         {
  82.             key = getch();
  83.             switch ( key )
  84.             {
  85.                 case 'a':
  86.                 case 'A':
  87.                     f.fheight = 0;
  88.                     break;
  89.                 case 'b':
  90.                 case 'B':
  91.                     f.fheight = 1;
  92.                     break;
  93.                 case 'c':
  94.                 case 'C':
  95.                     f.fheight = 2;
  96.                     break;
  97.                 case 'd':
  98.                 case 'D':
  99.                     f.fheight = 3;
  100.                     break;
  101.                 case 'p':
  102.                 case 'P':
  103.                     if ( picimage )
  104.                     {
  105.                         free(picimage);
  106.                         picimage = 0;
  107.                         FlameZeroScreen();
  108.                         FlamePal(fpal);
  109.                     }
  110.                     else
  111.                     {
  112.                         picimage = LoadPicture("image",imagepal);
  113.                         if ( picimage )
  114.                         {
  115.                             FlameDAC(imagepal);
  116.                             FlameColorMatch(&f,imagepal,fpal);
  117.                         }
  118.                     }
  119.                     break;
  120.                 case 'y':
  121.                     fheight+=20;
  122.                     if ( fheight > 200 ) fheight = 200;
  123.                     if ( fheight != f.height )
  124.                     {
  125.                         DestroyFlame(&f); // free frame buffers
  126.                         CreateFlame(&f,fwidth,fheight); // create new flame buffer
  127.                         FlameZeroScreen();
  128.                     }
  129.                     break;
  130.                 case 'Y':
  131.                     fheight-=20;
  132.                     if ( fheight < 20 ) fheight = 20;
  133.                     if ( fheight != f.height )
  134.                     {
  135.                         DestroyFlame(&f);
  136.                         CreateFlame(&f,fwidth,fheight);
  137.                         FlameZeroScreen();
  138.                     }
  139.                     break;
  140.  
  141.                 case 'x':
  142.                     fwidth+=20;
  143.                     if ( fwidth > 320 ) fwidth = 320;
  144.  
  145.                     if ( fwidth != f.width )
  146.                     {
  147.                         DestroyFlame(&f); // free frame buffers
  148.                         CreateFlame(&f,fwidth,fheight); // create new flame buffer
  149.                         FlameZeroScreen();
  150.                     }
  151.  
  152.                     break;
  153.                 case 'X':
  154.                     fwidth-=20;
  155.                     if ( fwidth < 20 ) fwidth = 20;
  156.                     if ( fwidth != f.width )
  157.                     {
  158.                         DestroyFlame(&f);
  159.                         CreateFlame(&f,fwidth,fheight);
  160.                         FlameZeroScreen();
  161.                     }
  162.                     break;
  163.  
  164.                 case 'f':
  165.                 case 'F':
  166.                     f.fire8 = 1-f.fire8;
  167.                     break;
  168.                 case '0':
  169.                 case '1':
  170.                 case '2':
  171.                 case '3':
  172.                 case '4':
  173.                 case '5':
  174.                 case '6':
  175.                 case '7':
  176.                 case '8':
  177.                 case '9':
  178.                     f.fuelbase = (key-'0')*12;
  179.                     break;
  180.                 case '+':
  181.                     fuel = f.fuel+20;
  182.                     if ( fuel > 255 ) fuel = 255;
  183.                     break;
  184.                 case '-':
  185.                     fuel = f.fuel-20;
  186.                     if ( fuel < 0 ) fuel = 0;
  187.                     break;
  188.                 case ']':
  189.                     mask++;
  190.                     if ( mask > 9 ) mask = 9;
  191.                     f.andmask = masktable[mask];
  192.                     break;
  193.                 case '[':
  194.                     mask--;
  195.                     if ( mask < 0 ) mask = 0;
  196.                     f.andmask = masktable[mask];
  197.                     break;
  198.                 case 27:
  199.                     xcon = 1;
  200.                     break;
  201.             }
  202.         }
  203.     } while ( !xcon );
  204.  
  205.     DestroyFlame(&f);
  206.     free(buildframe);
  207.     if ( picimage ) free(picimage);
  208.  
  209.     FlameStop();
  210.  
  211. }
  212.  
  213. // create a flame buffer.
  214. int CreateFlame(FLAMESPEC *f,long wid,long hit)
  215. {
  216.     int i,j;
  217.  
  218.     f->width = wid;
  219.     f->height = hit;
  220.     f->fsize = wid*hit;
  221.     f->fheight = 1;
  222.     f->fuel = 100+hit;
  223.     if ( f->fuel > 255 ) f->fuel = 255;
  224.     f->fire8 = 0;
  225.     f->ScreenX = (320-wid)/2; // center on screen
  226.     f->ScreenY = 200-hit; // put on bottom.
  227.     f->jitter = malloc(sizeof(long)*512);
  228.     if ( !f->jitter ) return(0);
  229.  
  230.     // make the 512 entry random jitter table.
  231.     for (i=0; i<512; i++)
  232.     {
  233.         j = rand()%f->width; // assign valid random jitter value.
  234.         if ( j < 1 ) j = 1;
  235.         if ( j > (f->width-2) ) j = (f->width-2);
  236.         f->jitter[i] = j;
  237.     }
  238.  
  239.     f->seed = 0;
  240.     f->andmask = 0x31;
  241.     f->fuelbase = 64;
  242.  
  243.     f->flame1 = malloc(f->fsize);
  244.     if ( !f->flame1 )
  245.     {
  246.         free(f->jitter);
  247.         return(0);
  248.     }
  249.     f->flame2 = malloc(f->fsize);
  250.     if ( !f->flame2 )
  251.     {
  252.         free(f->flame1);
  253.         free(f->jitter);
  254.         return(0);
  255.     }
  256.     memset(f->flame1,0,f->fsize);
  257.     memset(f->flame2,0,f->fsize);
  258.  
  259.     return(1);
  260. }
  261.  
  262. void DestroyFlame(FLAMESPEC *f)
  263. {
  264.     if ( f->flame1 ) free(f->flame1);
  265.     if ( f->flame2 ) free(f->flame2);
  266.     if ( f->jitter ) free(f->jitter);
  267.     f->flame1 = f->flame2 = 0;
  268. }
  269.  
  270. char * LoadPicture(char *fname,char *imagepal)
  271. {
  272.     FILE *fph;
  273.     char name[256];
  274.     char *image;
  275.  
  276.     strcpy(name,fname);
  277.     strcat(name,".PAK");
  278.     fph = fopen(name, "rb");
  279.     if ( fph )
  280.     {
  281.         image = malloc(64000);
  282.         if ( !image ) return(0);
  283.         fread(image, 64000, 1, fph);
  284.         fclose(fph);
  285.         strcpy(name,fname);
  286.         strcat(name,".PAL");
  287.         fph = fopen(name, "rb");
  288.         if ( fph )
  289.         {
  290.             fread(imagepal, 768, 1, fph);
  291.             fclose(fph);
  292.             return(image);
  293.         }
  294.         free(image);
  295.         return(0);
  296.     }
  297.     return(0);
  298. }
  299.  
  300. void FlameColorMatch(FLAMESPEC *f,unsigned char *imagepal,unsigned char *fpal)
  301. {
  302.     long i,red,green,blue,j;
  303.  
  304.     for (i=0; i<256; i++)
  305.     {
  306.         red = fpal[i*3];
  307.         green = fpal[i*3+1];
  308.         blue = fpal[i*3+2];
  309.         j = FlameClosestColor(imagepal,red,green,blue);
  310.         f->ctrans[i] = j; // color translate.
  311.     }
  312. }
  313.